$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true,
			labelPosition:'left',
			queryIng : false, //正在查询
			queryData:{activeFlag:'1',page:1,rows:10}, //查询条件
			datagrid:[], //数据列表数据
			totalNum:0, //分页控件配置
			data:[],
			defaultCheckedKeys:[],
			addData:{
				goodsId:null,
				goodsName:null
			},
			selectedRow:null,
			isEdit:false,
			rules:{
				name: [{ required: true, message: '该项必填', trigger: 'change' }],
				location: [{ required: true, message: '该项必填', trigger: 'change' }],
				status: [{ required: true, message: '该项必填', trigger: 'change' }],
				type: [{ required: true, message: '该项必填', trigger: 'change' }],
				adImage: [{ required: true, message: '该项必填', trigger: 'change' }],
				startTime: [{ required: true, message: '该项必填', trigger: 'change' }],
				stopTime: [{ required: true, message: '该项必填', trigger: 'change' }],
				orderNum: [{ required: true, message: '该项必填', trigger: 'change' }],
			},
			dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
			},
			imageUrl_adImage:null,
			size:"mini",
			typeOptions:[{label:"店铺广告",value:"0"},{label:"分类广告",value:"1"},{label:"商品广告",value:"2"}],
			locationOptions:[{label:"顶部Banner",value:"0"},{label:"2号位",value:"1"},{label:"3号位",value:"2"}],
			shopList:[],
			cascaderProps:{
				value:"id",
				label:"itemName",
			},
			cascaderOptions:[],
		},
		components:{
			'goodsSelect': goodsSelectComponentJs
		},
		methods: {
			formatStatus:function(e){
				if(e=='0'){
					return '下线';
				}
				if(e=='1'){
					return '上线';
				}
			},
			formatType:function(e){
				for(var i=0;i<app.typeOptions.length;i++){
					if(e==app.typeOptions[i].value){
						return app.typeOptions[i].label
					}
				}
			},
			init:function () {
				var vm = this
				vm.query();
				vm.queryShop();
				vm.queryClassify();
			},
			queryShop:function(){
				var that = this;
				$.ajax({
					url :staticURL+'/mall/shop/listAll',
					data : {
						activeFlag:1
					},
					dataType : 'json',
					success : function(r) {
						that.shopList = r.data;
					}
				});
			},
			queryClassify:function(){
				var that = this;
				$.ajax({
					url :staticURL+'/mall/classify/listAll2',
					data : {
						queryLevel:0,
						activeFlag:1,
						rows:9999
					},
					dataType : 'json',
					success : function(json) {
						that.cascaderOptions = json.data;
					}
				});
			},
			//根据类型变更，重置其他类型所选的数据
			handleTypeData:function(type) {
				if(type == "1") {
					this.addData.classifyId = "";
					this.addData.classifyName = "";
					this.addData.goodsId = "";
					this.addData.goodsName = "";
				}else if(type == "2") {
					this.addData.shopId = "";
					this.addData.shopName = "";
					this.addData.goodsId = "";
					this.addData.goodsName = "";
				}else if(type == "3") {
					this.addData.shopId = "";
					this.addData.shopName = "";
					this.addData.classifyId = "";
					this.addData.classifyName = "";
				}
			},
			handleClassify:function(v){
				this.handleTypeData("2");
				
				var that = this;
				var parentId = v[0];
				var id = v[1];
				var id2 = v[2];
				for(var i=0;i<that.cascaderOptions.length;i++){
					if(parentId==that.cascaderOptions[i].id){
						for(var j=0;j<that.cascaderOptions[i].children.length;j++){
							if(id==that.cascaderOptions[i].children[j].id){
								for(var k=0;k<that.cascaderOptions[i].children[j].children.length;k++){
									if(id2==that.cascaderOptions[i].children[j].children[k].id){
										that.addData.classifyName = that.cascaderOptions[i].children[j].children[k].itemName;
										that.addData.classifyId = that.cascaderOptions[i].children[j].children[k].id;
										break;
									}
								}
								break;
							}
						}
						break;
					}
				}
			},
			selectGoods:function(g){
				this.handleTypeData("3");
				app.addData.goodsId = g.id;
				app.addData.goodsName = g.name;
			},
			search:function(){
				//点击查询按钮，默认查询第一页。
				this.queryData.page=1;
				this.query();
			},
			query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				$.ajax({
					url :staticURL+'/mall/advert/datagrid',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			},
			addItem:function(){
				app.openDialog();
			},
			deleteItem:function(row){
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
			        		url : staticURL+"/mall/advert/delete?ids="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				return;
				
			},
			editItem:function(row){
				app.openDialog(row);
			},
			handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			},
			handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			},
			handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			},
			resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    },
			submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					if(res){
						//验证通过
						$.ajax({
							url : app.saveUrl,
							data : app.addData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.dialogFormVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			},
			openDialog:function(param){
				this.imageUrl_adImage = null;
				if(param){
					app.isEdit = true;
					//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
					//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
					//app.addDatat同时被reset,会导致bug
					app.addData = JSON.parse(JSON.stringify(param)); 
					
					if(app.addData.adImage){
						this.imageUrl_adImage = app.addData.adImage;
					}
					this.typeChange(app.addData.type)
				}else{
					app.isEdit = false;
					app.addData = {
							orderNum:1
							,adImage:""
							,goodsId:null,
							goodsName:null,
							status:'1'
							};
				}
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
				
			},
			closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
				app.resetForm('dialogForm')
			},
			handleAvatarSuccess_adImage:function(res, file) {
				app.addData.adImage = res.imageUrl;
		        this.imageUrl_adImage = URL.createObjectURL(file.raw);
		    },
		    uploadUrl:function(){
				
				return staticURL+"/app/mytools/upload";
			},
		    beforeAvatarUpload:function(file) {

		        const isLt2M = file.size / 1024 / 1024 < 2;
	
		        if (!isLt2M) {
		          this.$message.error('上传图片大小不能超过 2MB!');
		        }
		        
		        return isLt2M;
		   },
		   datagridSelect:function(row, event, column){
				app.selectedRow = row;
		   },
		   handleShop:function(e){
			   this.handleTypeData("1");
			   var that = this;
			   for(var i=0;i<that.shopList.length;i++){
				   if(e == that.shopList[i].id){
					   that.addData.shopName = that.shopList[i].name;
					   break;
				   }
			   }
		   },
		   typeChange:function(e){
			  if(e==0){
				  this.rules.shopId=[{ required: true, message: '该项必填', trigger: 'change' }]
				  delete this.rules.classifyId;
				  delete this.rules.goodsId;
			  }else if(e==1){
				  this.rules.classifyId=[{ required: true, message: '该项必填', trigger: 'change' }]
				  delete this.rules.shopId;
				  delete this.rules.goodsId;
			  }else if(e==2){
				  this.rules.goodsId=[{ required: true, message: '该项必填', trigger: 'change' }]
				  delete this.rules.classifyId;
				  delete this.rules.shopId;
			  }
		   }
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/mall/advert/edit'
				}else{
					return staticURL+'/mall/advert/add'
				}
			},
			columnWidth:function(){

				let w = $(window).width();   

				w = w - 48-48-50-150;
				
				return Math.floor(w/5);
			}
		},
		created: function () {
			this.init();
		},
		updated:function(){
		}
	});
});